/**
  ******************************************************************************
  * @file    IWTG.c
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   This file contains  IWTG function
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/

#include "stm32f10x.h"
#include "iwtg.h"
#include "global.h"

enum TaskStatus {FAILED = 0, INIT, PASSED} TaskStatus;

/*****************************************************************************************
* Function:	void Iwatchdog_Init(void) 
*
* Abstract:	initial the Iwatchdong timer.
*
* Arguments:	none
*
* Returns:	none
*
* Global Variables Affected: none
*/
void Iwatchdog_Init()
{
	u8 i;

	// Set each task flag to UNKNOWN
	for (i = 0; i < TASK_MAX; i++) 
	{
		task_status[i] = UNKNOWN;
	}
	TaskStatus = INIT;

	/* IWDG timeout equal to 280 ms (the timeout may varies due to LSI frequency
	dispersion) */
	/* Enable write access to IWDG_PR and IWDG_RLR registers */
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

	/* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
	IWDG_SetPrescaler(IWDG_Prescaler_32);

	/* IWDG timeout = 2500/1250 = 2s */
	IWDG_SetReload(I_WATCHDOG_TIMEOUT);

	/* Reload IWDG counter */
	IWDG_ReloadCounter();

	/* Enable IWDG (the LSI oscillator will be enabled by hardware) */
	IWDG_Enable();
}
/*****************************************************************************************
* Function:	void iwatchdog_task(void) 
*
* Abstract:	This task checks the status of all other tasks and reloads the wwatchdog timer.
*
* Arguments:	none
*
* Returns:		tasks must not return
*
* Global Variables Affected: none
*/
 void iwatchdog_task(void const *argument) 
{
    u8 i;
    // Initialization 

    // Tasks must run in an infinite loop:
    while (TRUE)
    {

        if(TaskStatus == INIT)  
        {
            /* Reload IWDG counter */
            IWDG_ReloadCounter();
            TaskStatus = PASSED;
        }
        else if(TaskStatus == PASSED)  
        {
            for(i = 0; i < TASK_MAX; i++)  
            {
                if(task_status[i] == UNKNOWN) 
                {
                    TaskStatus = FAILED;
                    break;
                }
            }

            // Now if all tasks are alive, then reset the watchdog timer and
            // reset all task status to UNKNOWN.
            if(TaskStatus == PASSED)  
            {
                /* Reload IWDG counter */
                IWDG_ReloadCounter();

                // Set each task flag to UNKNOWN
                for(i = 0; i < TASK_MAX; i++)  
                {
                    task_status[i] = UNKNOWN;
                }
            }  // Later we will add code to store the number of the task that failed.
        }       
    	osDelay(RESET_IWDOG_ITV);
    }  // end while infinite loop
}  // end wwatchdog_task()

